app.controller("z-furlough-c", [ '$scope','$rootScope','$z-http','$sce','z-furlough-s','z-attendance-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		
		$scope.editModel = {};
		$scope.editModel.title="";
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'furlough'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			$scope.viewModel.furloughTypeData=[];
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"5"
			},
			function(data){
				//去除，调休0、年假1、产检假12、陪护假11
				if(data!=null){
					for(var i=0;i<data.length;i++){
						if(data[i].dicCd!=0
								&& data[i].dicCd!=1
								&& data[i].dicCd!=11
								&& data[i].dicCd!=12){
							$scope.viewModel.furloughTypeData.push(data[i]);
						}
					}
				}
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			Service.getFurloughList({
			},
			function(data){
				$scope.viewModel.dataList = data;
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.user={};
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.viewModel.load();
			$scope.editModel.title="新增休假设置";
			$scope.editModel.type = true;
			$scope.editModel.furlough={};
			//验证不能为空
			$scope.editForm.furloughType.$dirty=false;
			$scope.editForm.dayNumber.$dirty=false;
		};
		
		//修正
		$scope.viewModel.update=function(furlough){
			$scope.viewModel.load();
			$scope.editModel.title="修改休假设置";
			$scope.editModel.type = false;
			$scope.editModel.furlough=angular.copy(furlough);
		};
		
		//验证
		$scope.viewModel.validForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.furloughType.$dirty){
            		$scope.editForm.furloughType.$dirty=true;
            	}
            	if(!$scope.editForm.dayNumber.$dirty){
            		$scope.editForm.dayNumber.$dirty=true;
            	}
                return true;
            }else{
            	return false;
            }
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.validForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveFurlough($scope.editModel.furlough,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindSelectData();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateFurlough($scope.editModel.furlough,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindSelectData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.del=function(furlough){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteFurlough({
					furloughId:furlough.furloughId
				},function(){
					$scope.viewModel.bindSelectData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		$scope.viewModel.checkGlobal=function(){
			return false;
		};
} ]);